package com.thinkgem.jeesite.modules.shop.jiekou;

import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.security.shiro.session.SessionDAO;
import com.thinkgem.jeesite.common.servlet.ValidateCodeServlet;
import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.common.utils.IdGen;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.security.FormAuthenticationFilter;
import com.thinkgem.jeesite.modules.sys.security.SystemAuthorizingRealm;
import com.thinkgem.jeesite.modules.sys.security.UsernamePasswordToken;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping("${frontPath}/shop")
public class userLoginController extends BaseController {

    @Autowired
    private SessionDAO sessionDAO;


    @RequestMapping("login")
    public String login(){
        return "modules/moblieshop/person/denglu";
    }

    @RequestMapping("logout")
    public String logout(){
        UserUtils.getSubject().logout();
        return "modules/moblieshop/person/denglu";
    }



    @RequestMapping(value = "bb")
    @ResponseBody
    public Map<String,Object> loginFail(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("6666666666666");
        Map<String,Object> map = Maps.newHashMap();
        SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
        // 如果已经登录，则跳转到首页
        if(principal != null){
            map.put("status","2");
            map.put("message","已登陆，请勿重复登陆");
            return map;
        }
        String username = WebUtils.getCleanParam(request, FormAuthenticationFilter.DEFAULT_USERNAME_PARAM);
        boolean rememberMe = WebUtils.isTrue(request, FormAuthenticationFilter.DEFAULT_REMEMBER_ME_PARAM);
        boolean mobile = WebUtils.isTrue(request, FormAuthenticationFilter.DEFAULT_MOBILE_PARAM);
        String pass = request.getParameter("password");
        System.out.println(username);
        System.out.println(pass.toCharArray());
        String host = StringUtils.getRemoteAddr(request);
        try {
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,
                    pass.toCharArray(),rememberMe,host,null,mobile);
            SecurityUtils.getSubject().login(usernamePasswordToken);
            System.out.println(usernamePasswordToken);
            System.out.println("wode");
        }catch (Exception e){
            map.put("status","1");
            map.put("message","账号或密码错误，登录失败");
            return map;
        }
        User user = UserUtils.getUser();
        System.out.println("yonghu"+user);
        if(user != null && StringUtils.isNotEmpty(user.getId())){
            map.put("status","0");
            map.put("message","登录成功");
        }
        System.out.println(map);
        return map;
    }

    /**
     * 是否是验证码登录
     * @param useruame 用户名
     * @param isFail 计数加1
     * @param clean 计数清零
     * @return
     */
    @SuppressWarnings("unchecked")
    public static boolean isValidateCodeLogin(String useruame, boolean isFail, boolean clean){
        Map<String, Integer> loginFailMap = (Map<String, Integer>) CacheUtils.get("loginFailMap");
        if (loginFailMap==null){
            loginFailMap = Maps.newHashMap();
            CacheUtils.put("loginFailMap", loginFailMap);
        }
        Integer loginFailNum = loginFailMap.get(useruame);
        if (loginFailNum==null){
            loginFailNum = 0;
        }
        if (isFail){
            loginFailNum++;
            loginFailMap.put(useruame, loginFailNum);
        }
        if (clean){
            loginFailMap.remove(useruame);
        }
        return loginFailNum >= 3;
    }

}
